<%@page import="models.User"%>
<%@include file="template/configuration.jsp" %>
<%@page import="models.Category"%>
<%@page import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || ${uq_course_catalog}</title>
        ${stylesheet}
        ${jquery}
        ${jstooltip}
        ${jscollapse}
        ${jspagination}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <c:if test="${sessionScope.userrole == 'Teacher' || sessionScope.userrole == 'Administrator'}">
                    <div class="edit">
                        <a href="${url}Category?edit"><img src="${url}images/edit.png" /></a> 
                    </div>
                </c:if>
                <h1>${uq_course_catalog}</h1>
                <!-- clear //-->
                <div class="clear margin-bottom-10"></div>
                <c:set var="first" value="true" />
                <c:choose>
                    <c:when test="${fn:length(categories)!=0}">
                        <c:forEach items="${categories}" var="category">
                            <c:set var="catid" value="${category.id}" /> 
                            <%
                            // hu(mc), cd(mc), mc = manage categories
                            HibernateUtil humc = new HibernateUtil();
                            CourseDao cdmc = new CourseDao(humc.openSession());
                            HttpSession sessie = request.getSession();
                            List<Course> courseListMC = null;
                            courseListMC = cdmc.findAllByCategory(Long.parseLong(pageContext.getAttribute("catid").toString()), 4);
                            request.setAttribute("courses", courseListMC);           
                            if (courseListMC.size() > 0) {
                            %>
                                <div class="bundle" <c:if test="${first != 'true'}">style="margin-top: -50px;"</c:if>>
                                    <c:set var="first" value="false" />
                                    <div class="top">
                                        <div class="left">
                                            <c:if test="${sessionScope.userrole == 'Administrator'}">
                                                <a href="${url}Category?id=${category.id}"><img src="${url}images/edit.png" /></a> 
                                                <a href="${url}Category?idremove=${category.id}" class="remove"><img src="${url}images/remove.png" /></a>
                                                <span class="info">${uq_category} <strong>${category.name}</strong></span>
                                            </c:if>
                                        </div>
                                        <div class="center">
                                            <h5>${category.name}</h5>
                                        </div>
                                        <div class="right">
                                            <c:if test="${sessionScope.userrole == 'Administrator'}">
                                                <a href="ManageCourse/form?catid=${category.id}">${uq_add_a_new_course}<img src="${url}images/add.png" /></a>
                                            </c:if>
                                        </div>
                                    </div>
                                    <!-- clear //-->
                                    <div class="clear"></div>
                                    <div class="full-block" style="min-height: 26px;">
                                        <div style="display: none;">
                                            <c:forEach items="${courses}" var="course">
                                                <fieldset class="inside-block">
                                                    <legend align="center">
                                                        <div class="button no-link"><a>${course.name}</a></div>
                                                    </legend>
                                                    <div class="left">
                                                        <img src="${url}images/course-default.png" />
                                                    </div>
                                                    <div class="right">
                                                        <h4>${uq_description}</h4>
                                                        ${course.description}
                                                    </div>
                                                    <div class="bottom">
                                                        <div class="button float-right"><a href="${url}Course?courseid=${course.id}">${uq_view_course}</a></div>
                                                        <c:set var="owned" value="false" />
                                                        <c:forEach items="${ownedCourses}" var="mycourse">
                                                            <c:if test="${mycourse.id == course.id}">
                                                                <c:set var="owned" value="true" />
                                                            </c:if>
                                                        </c:forEach>
                                                        <c:if test="${owned == 'false'}">
                                                            <c:choose>
                                                                <c:when test="${sessionScope.cart==null}">
                                                                    <div class="button float-right"><a href="CartController?courseid=${course.id}" class="add">${uq_add_to_cart}</a></div>                                                                
                                                                </c:when>
                                                                <c:otherwise>
                                                                    <c:set var="incart" value="false"/>
                                                                    <c:forEach items="${sessionScope.cart.cart}" var="cart">
                                                                        <c:if test="${cart.id==course.id}"><c:set var="incart" value="true"/></c:if>
                                                                    </c:forEach>
                                                                        <c:if test="${incart==true}">
                                                                    <div class="button float-right"><a href="CartController?courseid=${course.id}" class="remove-from-cart">${uq_remove_from_cart}</a></div>    
                                                                    </c:if>
                                                                    <c:if test="${incart==false}">
                                                                    <div class="button float-right"><a href="CartController?courseid=${course.id}" class="add">${uq_add_to_cart}</a></div>    
                                                                    </c:if>                                                                    
                                                                    <c:set var="incart" value="false"/>
                                                                </c:otherwise>
                                                            </c:choose>
                                                            <div class="button free-lesson float-right"><a href="CourseLesson?courseid=${course.id}&isFree=true">${uq_free_lesson}</a></div>
                                                        </c:if>
                                                    </div>
                                                </fieldset>
                                                <div class="clear margin-bottom-20"></div>
                                            </c:forEach>
                                            <%
                                            double total_items = cdmc.getLastPage(Long.parseLong(pageContext.getAttribute("catid").toString()));
                                            pageContext.setAttribute("pagination", (int) Math.ceil(total_items / 4));
                                            %>
                                            <c:if test="${pagination > 1}">
                                                <div class="pagination">
                                                    <c:forEach var="i" begin="1" end="${pagination}" step="1">
                                                        <a href="${url}jsp/pagination/course_catalog.jsp?page=${i}&category_id=${category.id}" class="number<c:if test="${i == '1'}"> active</c:if>">${i}</a>
                                                    </c:forEach>
                                                </div>
                                            </c:if>
                                        </div>
                                    </div>
                                    <!-- clear //-->
                                    <div class="clear margin-bottom-10" style="display: none;"></div>
                                </div>
                            <% } %>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <div class="full-block">
                            ${uq_there_are_no_categories_added_yet}
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
